﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EmptoolWebApp.Helpers
{
    public class AzureStorageHelper : BaseHelper
    {
        private static Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = null;

        private static Microsoft.WindowsAzure.Storage.Table.CloudTableClient tableClient = null;

        public static Microsoft.WindowsAzure.Storage.CloudStorageAccount GetStorageAccount()
        {
            if (storageAccount == null)
            {
                try
                {
                    storageAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(
                        Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));
                }
                catch (Exception)
                {

                    throw;
                }
            }

            return storageAccount;
        }

        public static Microsoft.WindowsAzure.Storage.Table.CloudTableClient GetStorageTableClient()
        {
            if (tableClient == null)
            {
                try
                {
                    tableClient = GetStorageAccount().CreateCloudTableClient();
                }
                catch (Exception)
                {

                    throw;
                }
            }

            return tableClient;
                
        }

        public static Microsoft.WindowsAzure.Storage.Table.CloudTable GetStorageTable(string tableName)
        {
            Microsoft.WindowsAzure.Storage.Table.CloudTable table = null;

            if (!string.IsNullOrWhiteSpace(tableName))
            {
                try
                {
                    // Retrieve a reference to the table.
                    table = GetStorageTableClient().GetTableReference(tableName);

                    // Create the table if it doesn't exist.
                    table.CreateIfNotExists();
                }
                catch (Exception)
                {
                    throw;
                }
            }

            return table;
        }
    }
}